package sis.security;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-31 11:44
 * @notify
 * @version 1.0
 */

import sis.studentinfo.Account;

import java.lang.reflect.Proxy;

//工厂类
public class AccountFactory {
    //自行判断是返回一个真正的Account类还是一个代理的类
    public static Accountable create(Permission permission) {
        switch (permission) {
            case UPDATE:
                return new Account();
            case READ_ONLY:
                return createSecuredAccount();
        }
        return null;
    }

    private static Accountable createSecuredAccount() {

        SecureProxy secureAccount = new SecureProxy(new Account(),
                "credit",
                "setBankAba",
                "setBankAccountNumber",
                "setBankAccountType",
                "transferFromBank");

        return //返回一个代理对象
                (Accountable) Proxy.newProxyInstance(
                        //类加载器
                Accountable.class.getClassLoader(),
                //实现 Accountable接口的所有方法.
                new Class[]{Accountable.class},
                        //传入真正的代理对象
                secureAccount);
    }
}
